﻿/*!
 * Copyright &copy; 2016-2020 <a href="http://www.jeeweb.cn/">JeeWeb</a> All rights reserved.
 * 
 * 通用公共方法
 * @author 王存见
 * @version 2016-12-04
 */
/**
 * 新增事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 */
function create(title,contenId,gridid,width,height) {
	openDialog(title,contenId,gridid,width,height);
}

/**
 * 新增事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 */
function createDialog(title,contenId,gridid,width,height) {
	openDialog(title,contenId,gridid,width,height);
}

/**
 * 更新事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 * @param id//主键字段
 */
function update(title,contenId,gridId,width,height) {
	var rowsData = $("#"+gridId).jqGrid('getGridParam','selarrrow');
	var multiselect=$("#"+gridId).jqGrid('getGridParam','multiselect');
	var rowData= $("#"+gridId).jqGrid('getGridParam','selrow');
	if(!multiselect)
	{
		if(rowData)
		{
			  rowsData[0]=rowData;
		}
	}
    if (!rowsData || rowsData.length==0) {
	    top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
		return; 
	}
    if (rowsData.length>1) {
    	top.layer.alert('只能选择一条数据!', {icon: 0, title:'警告'});
		return;
	}
    
    var id = rowsData[0];
    openDialog(title,contenId,gridId,width, height);
}

/**
 * 更新事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 * @param id//主键字段
 */
function updateDialog(title,contenId,gridId,width,height) {
	var rowsData = $("#"+gridId).jqGrid('getGridParam','selarrrow');
	var multiselect=$("#"+gridId).jqGrid('getGridParam','multiselect');
	var rowData= $("#"+gridId).jqGrid('getGridParam','selrow');
	if(!multiselect)
	{
		if(rowData)
		{
			  rowsData[0]=rowData;
		}
	}
    if (!rowsData || rowsData.length==0) {
	    top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
		return; 
	}
    if (rowsData.length>1) {
    	top.layer.alert('只能选择一条数据!', {icon: 0, title:'警告'});
		return;
	}
    
    openDialog(title,contenId,gridId,width, height);
}

//打开对话框(添加修改)
function openDialog(title,contenId,gridId,width,height){
	width = width?width:'800px';
	height = height?height:'500px';
	if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
		width='auto';
		height='auto';
	}else{//如果是PC端，根据用户设置的width和height显示。
	
	}
	layer.open({
	    type: 1,  
	    area: [width, height],
	    title: title,
        maxmin: true, //开启最大化最小化按钮
        content: $("#"+contenId) ,
	    btn: ['确定', '关闭'],
	    yes: function(index, layero){
	    	vm.saveOrUpdate();
		},
		cancel: function(index){ 
			vm.reload();
		}
	}); 	
	
}


/*
 * 搜索重置
 */
function reset(gridId) {
    $("#" + gridId + "Query").find(":input").val("");
    //运行搜索
    search(gridId);
}


/**
 *搜索
 * @param gridId
 */
function search(gridId) {
    var queryParams = {};
    var queryFields=$('#queryFields').val();
    queryParams['queryFields'] = queryFields;
    //普通的查询
    $('#' + gridId + "Query").find(":input").each(function() {
		var val = $(this).val();
		if (queryParams[$(this).attr('name')]) {
			val = queryParams[$(this).attr('name')] + "," + $(this).val();
		}
		queryParams[$(this).attr('name')] = val;
	});

	// 普通的查询
	$('#' + gridId + "Query").find(":input").each(function() {
		var condition = $(this).attr('condition');
		if (!condition) {
			condition = "";
		}
		var key = "query." + $(this).attr('name') + "||" + condition;
		queryParams[key] = queryParams[$(this).attr('name')];
	});
    //刷新
    //传入查询条件参数  
    $("#"+gridId).jqGrid('setGridParam',{  
        datatype:'json',  
        postData:queryParams, //发送数据  
        page:1  
    }).trigger("reloadGrid"); //重新载入    
}

/**
 * 表单验证
 * @author han
 * @param formConfig
 * @returns
 */
function valForm(formConfig){
	var url = "";
	var id = "";
	var beforeCheck = function(r){};
	var beforeSubmit = function(r){};
	var callback = function(r){};	
	
	if (typeof(formConfig.url) != "undefined")
	{
		url = formConfig.url;
	}

	if (typeof(formConfig.id) != "undefined")
	{
		id = formConfig.id;
	}
	
	if (typeof(formConfig.beforeCheck) != "undefined")
	{
		beforeCheck = formConfig.beforeCheck;
	}
	
	if (typeof(formConfig.beforeSubmit) != "undefined")
	{
		beforeSubmit = formConfig.beforeSubmit;
	}
	
	if (typeof(formConfig.callback) != "undefined")
	{
		callback = formConfig.callback;
	}
	
	var formVal = $("#"+formConfig.id).Validform({
		tiptype:2, 
		ignoreHidden:false,
		dragonfly:false,
		tipSweep:true,
		label:".Validform_checktip",
		showAllError:false,
		postonce:true,
		url : formConfig.url,
		ajaxPost:false,
		tiptype:function(msg,o,cssctl){
			if(!o.obj.is("form")){
				var objtip=o.obj.siblings(".Validform_checktip");
				cssctl(objtip,o.type);
				objtip.text(msg);
			}
		},
		beforeCheck:function(curform){
			beforeCheck(curform);
		},
		beforeSubmit:function(curform){
			beforeSubmit(curform);
		},
		callback:function(r){
			callback(r);
		}	    		
	});
	return formVal;
}




